﻿using System;
using System.Collections.Generic;
using Ext.Net;
using BLL;
using Model;
using System.Xml;
using System.Xml.Xsl;

namespace Web.View.messagesMenu
{
	public partial class Default : UIPage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            
		}
		//绑定Grid
		protected void messages_DataBind(object sender, StoreRefreshDataEventArgs e)
		{
			messagesMgr mgr = new messagesMgr();
			e.Total = int.Parse(mgr.GetRecordCount_messages(e.Parameters[messages_Filter.ParamPrefix]));
            IList<messages> mList = null;
            if (Session["role"].ToString() != "1")
            {
                mList = mgr.FindByConditions_messages("toun = " + Session["currentUserID"]);
            }
            else
            {
                mList = mgr.FindAll_messages();
            }
            messages_MainStore.DataSource = mList;
            messages_MainStore.DataBind();           

		}
        //绑定Grid
        protected void messages_DataBind2(object sender, StoreRefreshDataEventArgs e)
        {
            userinfoMgr ugr = new userinfoMgr();
            e.Total = int.Parse(ugr.GetCount_userinfo(e.Parameters[GridFilters1.ParamPrefix]));
            IList<userinfo> list = ugr.FindAllByPage_userinfo(e.Start.ToString(), e.Limit.ToString(), "");
            message_DialogStore.DataSource = list;
            message_DialogStore.DataBind();
        }
		//删除数据
		protected void BtnDel_Click(object sender, DirectEventArgs e)
		{
			messagesMgr mgr = new messagesMgr();
			RowSelectionModel sm = messages_Grid.SelectionModel.Primary as RowSelectionModel;
			foreach (SelectedRow row in sm.SelectedRows)
			{
				messages _messages = new messages();
				_messages.id = int.Parse(row.RecordID);
                CheckBoxGrid.DeselectRow(row.RowIndex);
				if (mgr.Del_messages(_messages))
				{
					log.Debug("messages中执行了删除，并成功");
					X.Msg.Notify("成功", "<font color='green'>已经成功删除选中的记录</font>").Show();
				}
				else
					X.Msg.Notify("失败", "<font color='green'>删除记录失败，请您重试！</font>").Show();
			}
			messages_Grid.Reload();
		}
        protected void BtnAdd_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = send_Grid.SelectionModel.Primary as RowSelectionModel;
            sm.SelectedRows.Clear();
            sm.UpdateSelection();
        }
        protected void BtnResponse_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = send_Grid.SelectionModel.Primary as RowSelectionModel;
            //sm.SelectedRows.Add(new SelectedRow(Sender_Name.Text));
            sm.SelectedRows.Clear();
            sm.UpdateSelection();
            sm.SelectedRows.Add(new SelectedRow(MessageFrom.Value.ToString()));
            sm.UpdateSelection();
            messages_Win.Show();
            messages_Detail.Hide();
        }
		protected void BtnSave_Click(object sender, DirectEventArgs e)
		{
			messagesMgr mgr = new messagesMgr();
			messages _messages = new messages();
            RowSelectionModel sm = send_Grid.SelectionModel.Primary as RowSelectionModel;
            SelectedRow R = new SelectedRow(); 
            foreach (SelectedRow row in sm.SelectedRows)
            {
                if (Hid.Text.Length > 0)
                    _messages = mgr.FindById_messages(int.Parse(Hid.Text));
                
                _messages.fromun = (int)Session["currentUserID"];
                _messages.toun = int.Parse(row.RecordID);
                _messages.detail = Txtdetail.Text.Trim();
                _messages.ifread = false;
                _messages.time = System.DateTime.Now.ToString();
                _messages.sender = Session["username"].ToString();     
                CheckboxSelection1.DeselectRow(row.RowIndex);
                bool isok = false;
                if (Hid.Text.Length > 0)
                    isok = mgr.Update_messages(_messages);
                else
                    isok = mgr.Add_messages(_messages);
                if (isok)
                {
                    messages_Win.Hide();
                    X.Msg.Notify("成功", "<font color='green'>恭喜您，操作成功！</font>").Show();
                    log.Debug("messages中执行了保存（插入或修改），并成功");
                }
                else
                    X.Msg.Notify("失败", "<font color='red'>操作失败，请您重试！</font>").Show();
            }
           // R.RecordID = ;
            messages_Grid.Reload();
		}
        //数据保存为Excel
        protected void BtnSaveToExcel(object sender, EventArgs e)
		{
			string json = ExcelGridData.Value.ToString();
			StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
			XmlNode xml = eSubmit.Xml;
			this.Response.Clear();
			this.Response.ContentType = "application/vnd.ms-excel";
			this.Response.AddHeader("Content-Disposition", "attachment; filename=QiDian10Excel.xls");
			XslCompiledTransform xtExcel = new XslCompiledTransform();
			xtExcel.Load(Server.MapPath("/View/ExcelTemp/Excel.xsl"));
			xtExcel.Transform(xml, null, this.Response.OutputStream);
			this.Response.End();

		}
	}
}